import java.util.Date;

import org.hibernate.annotations.Parent;

import aiball.Contents;
import aiball.League;
import aiball.LeagueTeamPoints;
import aiball.UserAccount;
import aiball.UserAccountUserRoles;
import aiball.UserRoles;
import aiball.Team;
import aiball.Player;

class BootStrap {

	def springSecurityService


	def init = { servletContext ->

		def userRole = UserRoles.findByAuthority('ROLE_USER') ?: new UserRoles(authority: 'ROLE_USER').save(failOnError: true)
		def adminRole = UserRoles.findByAuthority('ROLE_ADMIN') ?: new UserRoles(authority: 'ROLE_ADMIN').save(failOnError: true)

		def defaultPlayer = Player.findByName('DefaultPlayer') ?: new Player(
				name: "DefaultPlayer",
				sourceCode: "none"
				).save(failOnError: true)


		def defaultTeam = Team.findByName('DefaultTeam') ?: new Team(
				name: "DefaultTeam",
				player1: defaultPlayer,
				player2: defaultPlayer,
				player3: defaultPlayer,
				player4: defaultPlayer,
				player5: defaultPlayer,
				logoURL: "none",
				description: "Default Team",
				warcry: "DEFAULT CRY",
				model: 1
				).save(failOnError: true)

		def adminUser = UserAccount.findByUsername('admin') ?: new UserAccount(
				username: 'admin',
				password: 'admin',
				email: 'admin@javakicker.de',
				team: defaultTeam,
				lastActivity: new Date(),
				enabled: true).save(failOnError: true)



		if (!adminUser.authorities.contains(adminRole)) {
			UserAccountUserRoles.create adminUser, adminRole
		}

		def defaultLeague = League.findByName('Hauptliga')?: new League(
				name: "Hauptliga",
				description:"",
				password:"",
				admin: adminUser
				).save(failOnError: true)

		
				
		def noParent = Contents.findByName('No Parent') ?: new Contents(
				name: "No Parent",
				code: "",
				headline: "",
				time: new Date(),
				userOnly: false,
				justCategory: true
				).save(failOnError: true);

		def tut = Contents.findByName('Tutorials') ?: new Contents(
				name: "Tutorials",
				code: "",
				headline: "Tutorials",
				time: new Date(),
				userOnly: false,
				justCategory: false,
				parent: noParent
				).save(failOnError: true);

		def dl = Contents.findByName('Downloads') ?: new Contents(
				name: "Downloads",
				code: "",
				headline: "Downloads",
				time: new Date(),
				userOnly: false,
				justCategory: false,
				parent: noParent
				).save(failOnError: true);

		def imp = Contents.findByName('Impressum') ?: new Contents(
				name: "Impressum",
				code: "" ,
				headline: "Impressum",
				time: new Date(),
				userOnly: false,
				justCategory: false,
				parent: noParent
				).save(failOnError: true);
			
			
								
	}
	def destroy = {
	}
																		
}
